package biznussoft.metercor.lookup;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;
import biznussoft.metercor.R;
import biznussoft.metercor.entity.ItemModel;
import biznussoft.metercor.fragments.NewWaterMeterDataManagement;
import biznussoft.metercor.fragments.ServiceOrderRecordsFragment;
import biznussoft.metercor.utils.Session;

public class ModelLookup  implements OnItemClickListener{

	LookupAdapter adapter;
	Dialog lookupDialog;
	boolean isFiltered;
	private ArrayList<ItemModel> data;  
	private ArrayList<ItemModel> filterList;
	private ListView lvLookup;
	private EditText etLookup;
	private Context mContext;
	String strCategory;

	public void getItemLookUp(Context context,String category){               
		mContext = context;
		strCategory = category;
		lookupDialog = new Dialog(mContext);
		lookupDialog.setContentView(R.layout.accountlookup);
		lookupDialog.setTitle("FieldService360");
		lvLookup = (ListView) lookupDialog.findViewById(R.id.lvAccountlookuplistview);
		etLookup = (EditText) lookupDialog.findViewById(R.id.etFilteraccount);
		adapter = new LookupAdapter(ServiceOrderRecordsFragment.modelList);
		try{
			Log.i("RGT","Clicked1");
			if(ServiceOrderRecordsFragment.modelList.size()>0){
				Log.i("RGT","Clicked2");
				lvLookup.setAdapter(adapter);}
			etLookup.addTextChangedListener(new TextWatcher() {
				public void onTextChanged(CharSequence s, int start, int before, int count) {

					adapter.getFilter().filter(s);

				}
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {

				}
				public void afterTextChanged(Editable s) {

				}
			});              

			lookupDialog.show();

			lvLookup.setOnItemClickListener(this);
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	class LookupAdapter extends BaseAdapter implements Filterable{

		private  ValueFilter valueFilter;

		ArrayList<ItemModel> mStringFilterList = new ArrayList<ItemModel>();
		public LookupAdapter(ArrayList<ItemModel> list) {
			data = list;
			mStringFilterList = list;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		class ViewHolder{
			public TextView name,age;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			ViewHolder holder;
			if(v== null){
				holder = new ViewHolder();
				LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(android.R.layout.simple_list_item_1, null);
				holder.name = (TextView) v.findViewById(android.R.id.text1);
				v.setTag(holder);
			}else{
				holder = (ViewHolder) v.getTag();
			}

			ItemModel b = data.get(position);
			holder.name.setText(b.getName());


			return v;
		}


		public Filter getFilter() {
			if(valueFilter==null) {

				valueFilter=new ValueFilter();
			}

			return valueFilter;
		}

		private class ValueFilter extends Filter
		{

			@Override
			protected FilterResults performFiltering(CharSequence constraint)
			{
				FilterResults results=new FilterResults();

				if(constraint!=null && constraint.length()>0){

					filterList=new ArrayList<ItemModel>();

					for(int i=0;i<mStringFilterList.size();i++){

						String mLastName = mStringFilterList.get(i).getName();
						if(mLastName.toLowerCase().contains(constraint.toString()))
						{
							filterList.add(mStringFilterList.get(i));

						}
					}
					results.count=filterList.size();
					results.values=filterList;
					isFiltered = true;
				}else{
					isFiltered = false;
					results.count=mStringFilterList.size();
					results.values=mStringFilterList;

				}

				return results;
			}

			@SuppressWarnings("unchecked")
			@Override
			protected void publishResults(CharSequence constraint,
					FilterResults results) 
			{
				data = (ArrayList<ItemModel>) results.values;

				notifyDataSetChanged();

			}


		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.lvAccountlookuplistview:
			ItemModel bean  = data.get(position);
			Session.sModelId = bean.getId();
//			NewERTDeviceInstallationFragment.etModel1.setText(bean.getName());
//			NewERTDeviceInstallationFragment.etModel2.setText(bean.getName());
			NewWaterMeterDataManagement.customerSuppliedData2.setText(bean.getName());
			lookupDialog.dismiss();      
			break;


		default:
			break;
		}


	}

}
